import Vue from 'vue'
import Vuex from 'vuex'
import http from './http'
import router from './router'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        usertotal: {},
        loggedIn: false
    },

    /*
    *usertotal{
      "phone_num": "13373338444",
      "user_code": "20190703460002",
      "roles": 1,
      "username": "zhaoqing",
      "nick_name": "青儿",
      "sex": "girl"
      }
    * */
    getters: {
        userCoreCode: state => {
            return state.usertotal.userCode;
        }
    },
    mutations: {
        setUsertotal(state, payload) {
            state.usertotal = payload;
        },

    },
    actions: {
        login({commit}, loginForm) {
            http({
                url: '/login',
                method: 'post',
                data: "userCode=" + loginForm.userCode + "&password=" + loginForm.password,
            }).then(res => {
                if (res.data.code !== '12099') {
                    router.push('/login_page');
                    throw "登录失败";
                }
                return http({
                    url: 'security/mono/usertotal',
                    method: 'get',
                })
            }).then(res => {
                commit('setUsertotal', res.data)
                return res;
            })

        },
        logout({commit}) {
            http({
                url: '/logout',
                method: 'get',
            }).then(res => {
                commit('setUsertotal', {})
                return res;
            });
        }
    }

})
